package java8.deom4;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.junit.Test;

public class Tests4 {

	@Test
	public void test1() {

		List<Integer> intList = new ArrayList<Integer>() {
			{
				add(1);
				add(2);
				add(3);
			}
		};
		List<Integer> a = intList.stream().collect(Collectors.toList());

		for (int i = 0; i < intList.size(); i++) {
			System.out.println(intList.get(i) == a.get(i));
		}
	}

	@Test
	public void test2() {

		Set<Integer> intSet = new HashSet<Integer>() {
			{
				add(4);
				add(3);
				add(2);
				add(1);
			}
		};
		Iterator<Integer> it = intSet.iterator();
		while (it.hasNext()) {
			Integer i = it.next();
			System.out.println(i);
		}

		System.out.println("----------------------");

		List<Integer> a = intSet.stream().collect(Collectors.toList());
		Iterator<Integer> aIt = a.iterator();
		while (aIt.hasNext()) {
			Integer i = aIt.next();
			System.out.println(i);
		}

	}

	/**
	 * sorted
	 */
	@Test
	public void test3() {

		Set<Integer> intSet = new HashSet<Integer>();

		intSet.add(4);
		intSet.add(3);
		intSet.add(2);
		intSet.add(1);

		Iterator<Integer> it = intSet.iterator();
		while (it.hasNext()) {
			Integer i = it.next();
			System.out.println(i);
		}

		System.out.println("----------------------");

		List<Integer> a = intSet.stream().sorted().collect(Collectors.toList());
		Iterator<Integer> aIt = a.iterator();
		while (aIt.hasNext()) {
			Integer i = aIt.next();
			System.out.println(i);
		}

	}

	/**
	 * maxBy
	 */
	@Test
	public void test4() {
		Function<String, Integer> mapper = str -> Integer.parseInt(str);
		String o = Stream.of("2", "3", "1").collect(Collectors.maxBy(Comparator.comparing(mapper))).get();
		System.out.println(o);

		Integer i = Stream.of(2, 3, 1).collect(Collectors.maxBy(Comparator.naturalOrder())).get();
		System.out.println(i);
	}

	/**
	 * partitioningBy
	 */
	@Test
	public void test5() {

		Predicate<Integer> p = item -> item % 2 == 0;
		Map<Boolean, List<Integer>> partition = Stream.of(1, 2, 3, 4).collect(Collectors.partitioningBy(p));

		List<Integer> trueList = partition.get(true);
		trueList.forEach(item -> System.out.println(item));

		System.out.println("-------------");

		List<Integer> falseList = partition.get(false);
		falseList.forEach(item -> System.out.println(item));

	}

	/**
	 * groupingBy
	 */
	@Test
	public void test6() {
		Function<String, String> f = item -> item.substring(0, 2);
		Map<String, List<String>> map = Stream.of("a-1", "a-2", "b-1", "b-2").collect(Collectors.groupingBy(f));

		map.forEach((k, v) -> {
			String key = k;
			System.out.println("key:" + key);
			v.forEach(item -> System.out.println(item));
			System.out.println("-------------");

		});
	}

	/**
	 * joining
	 */
	@Test
	public void test7() {
		String s = Stream.of("a", "b", "c", "d").collect(Collectors.joining(","));
		System.out.println(s);
	}

	/**
	 * counting
	 */
	@Test
	public void test8() {
		Function<String, String> f = item -> item.substring(0, 2);
		Map<String, Long> map = Stream.of("a-1", "a-2", "b-1", "b-2")
				.collect(Collectors.groupingBy(f, Collectors.counting()));

		map.forEach((k, v) -> {
			System.out.println("key:" + k);
			System.out.println("v:" + v);

		});
	}

	/**
	 * mapping
	 */
	@Test
	public void test9() {
		Function<String, String> f = item -> item.substring(0, 2);

		Function<String, String> ff = item -> item + ":";

		Map<String, List<String>> map = Stream.of("a-1", "a-2", "b-1", "b-2")
				.collect(Collectors.groupingBy(f, Collectors.mapping(ff, Collectors.toList())));

		map.forEach((k, v) -> {
			String key = k;
			System.out.println("key:" + key);
			v.forEach(item -> System.out.println(item));
			System.out.println("-------------");

		});

	}

	/**
	 * getOrDefault
	 * computeIfAbsent
	 */
	@Test
	public void test10() {
		Map<String, String> map = new HashMap<String, String>() {
			{
				put("a", "aa");
				put("b", "bb");
			}
		};
		
		String c=map.getOrDefault("aa", "c");
		System.out.println(c);
		
		Function<String, String> f=item -> item.substring(0, 1);
		
		Function<String, String> ff=item -> item+":::";
		
		String cc=map.computeIfAbsent("aa", ff);
		System.out.println(cc);
		
		
	}

}
